<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/13
 * Time: 8:42
 */
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
class Vote extends Common
{
    public function vote()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where['v.is_del'] = array('eq', 0);
            if (isset($pagedata['title']) && $pagedata['title'] != '') {
                $where['title'] = array('like', '%' . $pagedata['title'] . '%');
            }
            if (isset($pagedata['is_pass']) && $pagedata['is_pass'] != '') {
                $where['is_pass'] = array('like', '%' . $pagedata['is_pass'] . '%');
            }
            if (isset($pagedata['pass_time']) && $pagedata['pass_time'] != ''){
                $created_at=explode(' - ', $pagedata['pass_time']); 
                if (isset($created_at) && is_array($created_at)){
                    foreach($created_at as $k=>$v)
                    {
                       $created_at[$k] = strtotime($v);
                    }  
                } 
              $where['pass_time']=['between time',$created_at];      
            } 
            $count = Db::name('vote')->alias('v')->field('v.*,u.nickname')
                ->join('user u', 'v.uid=u.uid', 'left')->where($where)->count();
            $data = Db::name('vote')->alias('v')->field('v.*,u.nickname')
                ->join('user u', 'v.uid=u.uid', 'left')
                ->where($where)
                ->limit($startrow, $pagesize)
                ->order('id', 'desc')
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));
        } else {

            return view();
        }
    }

    /*
     * 审核
     */
    public function voteIsPass()
    {
        if (request()->isAjax()) {
            $postData = input('param.');
            $where = "id = {$postData['id']}";
            $data['is_pass'] = $postData['is_pass'];
            $data['pass_time'] = date('Y-m-d H:i:s');
            $res = Db::name('vote')->where($where)->update($data);
            if ($res) {
                if ($postData['is_pass'] == 1) {
                    send_user_mes($postData['uid'], '投票提醒', "您申请的投票标题" . $postData['title'] . " 已通过审核");
                } else {
                    send_user_mes($postData['uid'], "投票提醒", "您申请的投票标题" . $postData['title'] . "未通过审核，未通过审核" . $postData['reject_reasons'] . ",您可以重新在app内申请");
                }
                return 0;
            } else {
                return 1;
            }
        }
    }

    /*
     * 查看投票选项
     */
    public function voteOption()
    {
        $id = input('id', '', 'trim');
        $res = Db::name('vote_option')->field('vote_title,vote_image')->where('vote_id', $id)->select();
        $this->assign('res', $res);
        return view();
    }

    /*
     * 投票选项列表
     */
    public function option()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where = [];
            if (isset($pagedata['title']) && $pagedata['title'] != '') {
                $where['title'] = array('like', '%' . $pagedata['title'] . '%');
            }

            $count = Db::name('vote_option')->alias('vo')->field('vo.*,v.title')
                ->join('vote v', 'vo.vote_id=v.id', 'left')->where($where)->count();
            $data = Db::name('vote_option')->alias('vo')->field('vo.*,v.title')
                ->join('vote v', 'vo.vote_id=v.id', 'left')
                ->where($where)
                ->limit($startrow, $pagesize)
                ->order('id', 'desc')
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));
        } else {
            $res = Db::name('vote')->field('title')->select();
            $this->assign('res', $res);
            return view();
        }
    }

    /*
     * 订单列表
     */
    public function order()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where = [];
            if (isset($pagedata['order_sn']) && $pagedata['order_sn'] != '') {
                $where['order_sn'] = array('like', '%' . $pagedata['order_sn'] . '%');
            }
            if (isset($pagedata['order_status']) && $pagedata['order_status'] != '') {
                $where['order_status'] = array('like', '%' . $pagedata['order_status'] . '%');
            }
            if (isset($pagedata['create_time']) && $pagedata['create_time'] != '') {
                $created_at = explode(' - ', $pagedata['create_time']);
                if (isset($created_at) && is_array($created_at)) {
                    foreach ($created_at as $k => $v) {
                        $created_at[$k] = $v;
                    }
                }

                $where['vo.create_time'] = ['between', $created_at];
            }
            $count = Db::name('vote_order')->alias('vo')->field('vo.*,u.nickname')
                ->join('user u', 'vo.uid=u.uid', 'left')
                ->where($where)->count();
            $data = Db::name('vote_order')->alias('vo')->field('vo.*,u.nickname')
                ->join('user u', 'vo.uid=u.uid', 'left')
                ->where($where)
                ->limit($startrow, $pagesize)
                ->order('order_id', 'desc')
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));
        } else {
            $res = Db::name('vote_order')->field('order_sn')->select();
            $this->assign('res', $res);
            return view();
        }
    }

    /*
     * 奖项设置
     */
    public function prize()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where['is_del'] = array('eq', 0);
            if (isset($pagedata['prize_name']) && $pagedata['prize_name'] != '') {
                $where['prize_name'] = array('like', '%' . $pagedata['prize_name'] . '%');
            }

            $count = Db::name('vote_prize')->where($where)->count();
            $data = Db::name('vote_prize')
                ->where($where)
                ->limit($startrow, $pagesize)
                ->order('id', 'desc')
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));
        } else {
            return view();
        }
    }

    /*
     * 奖项添加
     */
    public function addPrize()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            unset($post['file']);
            if (!$post['article_thumb']) {
                return ['code' => 1, 'msg' => '请上传奖项缩略图'];
                exit;
            }

            $data = [
                'prize_name' => $post['prize_name'],
                'prize_number' => $post['prize_number'],
                'prize_price' => $post['prize_price'],
                'prize_image' => $post['article_thumb'],
                'create_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::name('vote_prize')->insert($data);
            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 2];
            }
        } else {

            return $this->fetch();
        }
    }

    /*
     * 奖品上架下架
     */
    public function isShow()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('vote_prize')->where('id', $post['id'])->setField('is_display', $post['is_display']);
            if ($res) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }

    public function voteIsShow()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('vote')->where('id', $post['id'])->setField('is_display', $post['is_display']);
            if ($res) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }

    /*
     * 商品设置
     */
    public function del()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('vote_prize')->where('id', $post['id'])->setField('is_del', 1);
            if ($res) {
                return '1';
            } else {
                return '2';
            }
        }
    }

    /*
     * 修改总票数
     */
    public function editVote()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $saveData = array(
                'vote_number' => $post['data']['content'],

            );

            $res = Db::name('vote_option')->where('id', $post['data']['id'])->update($saveData);

            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }


    //删除投票
    public function vote_del()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('vote')->where('id', $post['id'])->delete();
            if ($res) {
                $option = Db::name('vote_option')->where('vote_id', $post['id'])->delete();
                if ($option) {
                    return '1';
                }
            } else {
                return '2';
            }
        }
    }

    //删除某个选项
    public function option_del()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('vote_option')->where('id', $post['id'])->delete();
            if ($res) {
                return '1';

            } else {
                return '2';
            }
        }
    }

    /*
     * 修改奖项
     */
    public function editPrize()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            unset($post['file']);
            if (!$post['article_thumb']) {
                return ['code' => 1, 'msg' => '请上传商品缩略图'];
                exit;
            }

            $data = [
                'prize_name' => $post['prize_name'],
                'prize_price' => $post['prize_price'],
                'prize_image' => $post['article_thumb'],
                'prize_number' => $post['prize_number'],

            ];
            $res = Db::name('vote_prize')->where('id', $post['id'])->update($data);
            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 2];
            }
        } else {
            $id = Request::instance()->get();
            $data = Db::name('vote_prize')->where('id', $id['id'])->find();

            $this->assign('data', $data);
            return $this->fetch();
        }
    }
}